package omq.panel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import omq.screen.OmqTesterMainScreen;

import component.OmqButton;

import constant.OmqTesterClientConst;

public class EditPreferencesPanel extends OmqPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -427173616548718636L;
	
	private OmqButton saveBtn;
	
	protected JLabel userLbl ;
	protected JTextField userTxt ;	
	
	protected JPasswordField pwdTxt ;
	protected JLabel pwdLbl ;
	protected JTextField qMgrTxt ;
	protected JLabel qMgrLbl ;
	protected JTextField queueTxt ;
	protected JLabel queueLbl ;

	protected JLabel URLLbl ;
	protected JTextField URLTxt ;	

	private Preferences myPreferences = null;

	
	public EditPreferencesPanel(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		initialize();
	}

	public EditPreferencesPanel(LayoutManager arg0) {
		super(arg0);
		initialize();
	}

	public EditPreferencesPanel(boolean arg0) {
		super(arg0);
		initialize();
	}

	public EditPreferencesPanel() {
		super();
		initialize();
	}
	
	private void initialize(){
		setLayout(new GridBagLayout());
		myPreferences = Preferences.userNodeForPackage(OmqTesterMainScreen.class);
		//Creating the gridBagConstraints 
		GridBagConstraints constraints = new GridBagConstraints();
		//Creating the filler
		JLabel filler = new JLabel("");
		filler.setPreferredSize(new Dimension(350,21));
		
		constraints.anchor = GridBagConstraints.PAGE_START;
		//Creating the user name row
				
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 0;
		add(getUserLbl(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 0;
		add(getUserTxt(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 2;
		constraints.gridy = 0;
		add(filler);
		
		//Creating the password row
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 0;
		constraints.gridy = 1;
		add(getPwdLbl(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 1;
		add(getPwdTxt(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 2;
		constraints.gridy = 1;
		add(filler,constraints);

		//Creating the queue manager row
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 0;
		constraints.gridy = 2;
		add(getQMgrLbl(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 2;
		add(getQMgrTxt(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 2;
		constraints.gridy = 2;
		add(filler,constraints);
		
		//Creating the queue name row
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 0;
		constraints.gridy = 3;
		add(getQueueLbl(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 3;
		add(getQueueTxt(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 2;
		constraints.gridy = 3;
		add(filler,constraints);

		//Creating the server URL row
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 0;
		constraints.gridy = 4;
		add(getURLLbl(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 4;
		add(getURLTxt(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 2;
		constraints.gridy = 4;
		add(filler,constraints);
		
		
		//Creating the save button row
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets = new Insets(10,0,0,0);  //top padding
		constraints.ipady = 0; 
		constraints.weightx = 0.5;
		constraints.weighty = 1.0;
		constraints.gridx = 1;
		constraints.gridy = 7;
		add(getSaveBtn(),constraints);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weightx = 0.5;
		constraints.gridx = 1;
		constraints.gridy = 8;
		add(filler,constraints);
	}

	// user Label methods
	public JLabel getUserLbl() {
		if(userLbl == null){
			userLbl = new JLabel();;
			userLbl.setText("User :");
			userLbl.setPreferredSize(new Dimension(75,21));
		}
		return userLbl;
	}

	public JTextField getUserTxt() {
		if(userTxt == null){
			userTxt = new JTextField(15);
			userTxt.setPreferredSize(new Dimension(120,21));
			userTxt.setMaximumSize(new Dimension(120, 21));
			userTxt.setMinimumSize(new Dimension(120, 21));
			userTxt.setText(myPreferences.get(OmqTesterClientConst.PREF_USER_NAME,OmqTesterClientConst.PREF_USER_NAME));
		}
		return userTxt;
	}

	// URL Label methods
	public JLabel getURLLbl() {
		if(URLLbl == null){
			URLLbl = new JLabel();;
			URLLbl.setText("Server URL:");
			URLLbl.setPreferredSize(new Dimension(75,21));
		}
		return URLLbl;
	}

	public JTextField getURLTxt() {
		if(URLTxt == null){
			URLTxt = new JTextField(15);
			URLTxt.setPreferredSize(new Dimension(120,21));
			URLTxt.setMaximumSize(new Dimension(120, 21));
			URLTxt.setMinimumSize(new Dimension(120, 21));
			URLTxt.setText(myPreferences.get(OmqTesterClientConst.PREF_SERVER_URL,OmqTesterClientConst.PREF_SERVER_URL));
		}
		return URLTxt;
	}
	
	// password Label methods
	public JLabel getPwdLbl() {
		if(pwdLbl == null){
			pwdLbl = new JLabel();;
			pwdLbl.setText("Password :");
			pwdLbl.setPreferredSize((new Dimension(75,21)));
		}
		return pwdLbl;
	}

	public JPasswordField getPwdTxt() {
		if(pwdTxt == null){
			pwdTxt = new JPasswordField(15);
			pwdTxt.setSize(120,21);
			pwdTxt.setMaximumSize(new Dimension(120, 21));
			pwdTxt.setMinimumSize(new Dimension(120, 21));
			pwdTxt.setText(myPreferences.get(OmqTesterClientConst.PREF_PASSWORD,OmqTesterClientConst.PREF_PASSWORD));
		}
		return pwdTxt;
	}

	// qmgr Label methods
	public JLabel getQMgrLbl() {
		if(qMgrLbl == null){
			qMgrLbl = new JLabel();;
			qMgrLbl.setText("Queue manager :");
			qMgrLbl.setPreferredSize((new Dimension(120,21)));
		}
		return qMgrLbl;
	}

	public JTextField getQMgrTxt() {
		if(qMgrTxt == null){
			qMgrTxt = new JTextField(15);
			qMgrTxt.setSize(120,21);
			qMgrTxt.setMaximumSize(new Dimension(120, 21));
			qMgrTxt.setMinimumSize(new Dimension(120, 21));
			qMgrTxt.setText(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR));
		}
		return qMgrTxt;
	}

	// queue Label methods
	public JLabel getQueueLbl() {
		if(queueLbl == null){
			queueLbl = new JLabel();;
			queueLbl.setText("Queue name :");
			queueLbl.setPreferredSize(new Dimension(120,21));
		}
		return queueLbl;
	}

	public JTextField getQueueTxt() {
		if(queueTxt == null){
			queueTxt = new JTextField(15);
			queueTxt.setSize(120,21);			
			queueTxt.setMaximumSize(new Dimension(120, 21));
			queueTxt.setMinimumSize(new Dimension(120, 21));
			queueTxt.setText(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME));
		}
		return queueTxt;
	}
	
	// save button methods
	public OmqButton getSaveBtn() {
		if(saveBtn == null){
			saveBtn = new OmqButton("Save preferences");
			saveBtn.setSize(120,21);
			saveBtn.setMaximumSize(new Dimension(120, 21));
			saveBtn.setMinimumSize(new Dimension(120, 21));
			saveBtn.addActionListener(new ActionListener(){
				 public void actionPerformed(ActionEvent e){
					 String errMsg = "";
					if((getUserTxt().getText()).trim().length() == 0 || getUserTxt().getText() == null  ){
						errMsg += "Please type a valid user name\n";
					 }
					 if(getPwdTxt().getPassword() == null || getPwdTxt().getPassword().length == 0){
						errMsg += "Please type a valid password\n";
					 }
					 if(getQMgrTxt().getText() == null || getQMgrTxt().getText().trim().length() == 0){
						errMsg += "Please type a valid queue manager name\n";
					 }
					 if(getQueueTxt().getText() == null || getQueueTxt().getText().trim().length() == 0){
						errMsg += "Please type a valid queue name\n";
					 }
					 if(errMsg == "" ){
						 JOptionPane.showMessageDialog(new JFrame(),"<html><body>Saving changes, please wait ...</body></html>","Message",JOptionPane.INFORMATION_MESSAGE);
						 waitForInfo(true);
						 myPreferences.put(OmqTesterClientConst.PREF_USER_NAME,getUserTxt().getText());
						 myPreferences.put(OmqTesterClientConst.PREF_PASSWORD,new String(getPwdTxt().getPassword()));
						 myPreferences.put(OmqTesterClientConst.PREF_QUEUE_MGR,getQMgrTxt().getText());
						 myPreferences.put(OmqTesterClientConst.PREF_QUEUE_NAME,getQueueTxt().getText());
						 myPreferences.put(OmqTesterClientConst.PREF_SERVER_URL,getURLTxt().getText());
						 JOptionPane.showMessageDialog(new JFrame(),"<html><body>Preferences saved.</body></html>","Message",JOptionPane.INFORMATION_MESSAGE);
						 waitForInfo(false);
					 }else{
						 JOptionPane.showMessageDialog(new JFrame(), errMsg, "Error",JOptionPane.ERROR_MESSAGE);
					 }
				 }
			});
		}
		return saveBtn;
	}
	public void refreshPrefs(){
		getUserTxt().setText(myPreferences.get(OmqTesterClientConst.PREF_USER_NAME,OmqTesterClientConst.PREF_USER_NAME));
		getPwdTxt().setText(myPreferences.get(OmqTesterClientConst.PREF_PASSWORD,OmqTesterClientConst.PREF_PASSWORD));
		getQMgrTxt().setText(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_MGR,OmqTesterClientConst.PREF_QUEUE_MGR));
		getQueueTxt().setText(myPreferences.get(OmqTesterClientConst.PREF_QUEUE_NAME,OmqTesterClientConst.PREF_QUEUE_NAME));
	}


}
